package stage1.module2;

import java.util.Scanner;

public class Homework2 {
    public int[][] checkerboard = new int[16][16]; //创建棋盘
    public int turn = -1;  //-1表示黑棋回合，1表示白棋回合,0表示游戏结束

    //绘制当前棋盘
    public void drawCheckerboard(){
        //打印第一行即列号
        System.out.print("   ");
        for(int i = 0; i < 16; i++){
            System.out.printf("%x  ", i);
        }
        System.out.println();

        //打印剩余行
        for(int i = 0; i < 16; i++){
            //打印剩余行的行号
            System.out.printf("%x  ", i);
            //打印棋盘，无棋子打印+，黑棋打印黑，白棋打印白
            for(int j = 0;j < 16; j++){
                switch (checkerboard[i][j]){
                    case 0:
                        System.out.print("+  ");
                        break;
                    case -1:
                        System.out.print("黑 ");
                        break;
                    case 1:
                        System.out.print("白 ");
                        break;
                }
            }
            System.out.println();
        }

    }

    //提示黑白方下棋
    public void move(){
        Scanner sc = new Scanner(System.in);
        if(turn == -1){
            System.out.print("黑棋回合，");
        }else{
            System.out.print("白棋回合，");
        }

        System.out.println("请输入落子位置，行(a-f对应10-15)：");
        int row = sc.nextInt();
        //检查行号是否合法
        while(row < 0 || row > 15){
            System.out.println("超出棋盘范围，请重新输入");
            row = sc.nextInt();
        }

        System.out.println("列(a-f对应10-15)：");
        int column = sc.nextInt();
        //检查列号是否合法
        while(column < 0 || column > 15){
            System.out.println("超出棋盘范围，请重新输入");
            column = sc.nextInt();
        }

        //检查落子位置是否有棋子
        if(checkerboard[row][column] != 0){
            System.out.println("该位置已经有棋子了，请重新输入");
            move();
        }else{
            checkerboard[row][column] = turn;
            //检查是否获胜
            if(ifEnd(row, column)){
                if(turn == -1){
                    System.out.println("黑棋获胜");
                }else{
                    System.out.println("白棋获胜");
                }
                //游戏结束
                turn = 0;
            }else{
                //轮到对方回合
                turn = -turn;
            }
        }
    }

    //落子后判断是否结束
    private boolean ifEnd(int row, int column){
        //左边同色棋子个数
        int left = count(row, column, 0, -1);
        //右边同色棋子个数
        int right = count(row, column, 0, 1);
        //上面同色棋子个数
        int up = count(row, column, -1, 0);
        //下面同色棋子个数
        int dowm = count(row, column, 1, 0);
        //右上同色棋子个数
        int rightUp = count(row, column, -1, 1);
        //右下同色棋子个数
        int rightDowm = count(row, column, 1, 1);
        //左上同色棋子个数
        int leftUp = count(row, column, -1, -1);
        //左下同色棋子个数
        int leftDown = count(row, column, 1, -1);

        //计算各方向同色棋子个数
        int leftAndRight = left + right - 1;
        int upAndDown = up +dowm - 1;
        int rightUPAndLeftDown = rightUp + leftDown - 1;
        int rightDownAndLeftUp = rightDowm + leftUp - 1;

        //判断是否结束
        int[] count = {leftAndRight, upAndDown, rightUPAndLeftDown, rightDownAndLeftUp};
        for(int i: count){
            if(i >= 5){
                return true;
            }
        }
        return false;
    }

    //统计新落下的棋子某一方法同色棋子的个数
    //row和column表示新棋子的位置
    //rowDirection表示行的方法，columnDirection表示列的方向
    private int count(int row, int column, int rowDirection, int columnDirection){
        int count = 0;
        while (true){
            if(checkerboard[row][column] == turn){
                row += rowDirection;
                column += columnDirection;
                count++;
                if(row < 0 || row > 15 || column < 0 || column > 15){
                    return count;
                }
            }else{
                return count;
            }
        }
    }
}
